package me.yan.controller.admin;


import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import me.yan.constant.Types;
import me.yan.controller.BaseController;
import me.yan.dto.cond.ContentCond;
import me.yan.dto.cond.MetaCond;
import me.yan.pojo.ContentDomain;
import me.yan.pojo.MetaDomain;
import me.yan.utils.APIResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name="文章管理")
@Controller
@RequestMapping("/admin/article")
public class ArticleController extends BaseController {
    @Operation(summary = "发布文章页")
    @GetMapping(value = "/publish")
    public String newArticle(HttpServletRequest request, Model model) {
        MetaCond metaCond = new MetaCond();
        metaCond.setType(Types.CATEGORY.getType());
        List<MetaDomain> metas = metaService.getMetas(metaCond);
        request.setAttribute("categories", metas);
        model.addAttribute("commons", commons);
        model.addAttribute("adminCommons", adminCommons);
        return "admin/article_edit";
    }
    @Operation(summary = "文章页")
    @GetMapping(value = "")
    public String index(
            HttpServletRequest request,
            @Parameter(name = "page", description = "页数", required = false)
            @RequestParam(name = "page", required = false, defaultValue = "1")
            int page,
            @Parameter(name = "limit", description = "每页数量", required = false)
            @RequestParam(name = "limit", required = false, defaultValue = "15")
            int limit,
            Model model
    ){
        PageInfo<ContentDomain> articles = contentService.getArticlesByCond(new ContentCond(), page, limit);
        request.setAttribute("articles", articles);
        model.addAttribute("commons", commons);
        return "admin/article_list";
    }
    @Operation(summary = "文章编辑")
    @GetMapping(value = "/{cid}")
    public String editArticle(
            @Parameter(name = "cid", description = "文章编号", required = true)
            @PathVariable
            Integer cid,
            HttpServletRequest request,
            Model model
    ){
        ContentDomain content = contentService.getArticleById(cid);
        request.setAttribute("contents", content);
        MetaCond metaCond = new MetaCond();
        metaCond.setType(Types.CATEGORY.getType());
        List<MetaDomain> categories = metaService.getMetas(metaCond);
        request.setAttribute("categories", categories);
        request.setAttribute("active", "article");
        model.addAttribute("commons", commons);
        model.addAttribute("adminCommons", adminCommons);
        System.out.println("contents--->"+content);
        return "admin/article_edit";
    }
    @Operation(summary = "编辑保存文章")
    @PostMapping("/modify")
    @ResponseBody
    public APIResponse modifyArticle(
            HttpServletRequest request,
            @Parameter(name = "cid", description = "文章主键", required = true)
            @RequestParam(name = "cid", required = true)
            Integer cid,
            @Parameter(name = "title", description = "标题", required = true)
            @RequestParam(name = "title", required = true)
            String title,
            @Parameter(name = "titlePic", description = "标题图片", required = false)
            @RequestParam(name = "titlePic", required = false)
            String titlePic,
            @Parameter(name = "slug", description = "内容缩略名", required = false)
            @RequestParam(name = "slug", required = false)
            String slug,
            @Parameter(name = "content", description = "内容", required = true)
            @RequestParam(name = "content", required = true)
            String content,
            @Parameter(name = "type", description = "文章类型", required = true)
            @RequestParam(name = "type", required = true)
            String type,
            @Parameter(name = "status", description = "文章状态", required = true)
            @RequestParam(name = "status", required = true)
            String status,
            @Parameter(name = "tags", description = "标签", required = false)
            @RequestParam(name = "tags", required = false)
            String tags,
            @Parameter(name = "categories", description = "分类", required = false)
            @RequestParam(name = "categories", required = false, defaultValue = "默认分类")
            String categories,
            @Parameter(name = "allowComment", description = "是否允许评论", required = true)
            @RequestParam(name = "allowComment", required = true)
            Boolean allowComment
    ){
        ContentDomain contentDomain = new ContentDomain();
        contentDomain.setCid(cid);
        contentDomain.setTitle(title);
        contentDomain.setTitlePic(titlePic);
        contentDomain.setSlug(slug);
        contentDomain.setContent(content);
        contentDomain.setType(type);
        contentDomain.setStatus(status);
        contentDomain.setTags(tags);
        contentDomain.setCategories(categories);
        contentDomain.setAllowComment(allowComment ? 1 : 0);

        contentService.updateContentByCid(contentDomain);
        return APIResponse.success();
    }
    @Operation(summary = "发布新文章")
    @PostMapping(value = "/publish")
    @ResponseBody
    public APIResponse publishArticle(
            HttpServletRequest request,
            @Parameter(name = "title", description = "标题", required = true)
            @RequestParam(name = "title", required = true)
            String title,
            @Parameter(name = "titlePic", description = "标题图片", required = false)
            @RequestParam(name = "titlePic", required = false)
            String titlePic,
            @Parameter(name = "slug", description = "内容缩略名", required = false)
            @RequestParam(name = "slug", required = false)
            String slug,
            @Parameter(name = "content", description = "内容", required = true)
            @RequestParam(name = "content", required = true)
            String content,
            @Parameter(name = "type", description = "文章类型", required = true)
            @RequestParam(name = "type", required = true)
            String type,
            @Parameter(name = "status", description = "文章状态", required = true)
            @RequestParam(name = "status", required = true)
            String status,
            @Parameter(name = "tags", description = "标签", required = false)
            @RequestParam(name = "tags", required = false)
            String tags,
            @Parameter(name = "categories", description = "分类", required = false)
            @RequestParam(name = "categories", required = false, defaultValue = "默认分类")
            String categories,
            @Parameter(name = "allowComment", description = "是否允许评论", required = true)
            @RequestParam(name = "allowComment", required = true)
            Boolean allowComment
    ){
        ContentDomain contentDomain = new ContentDomain();
        contentDomain.setTitle(title);
        contentDomain.setTitlePic(titlePic);
        contentDomain.setSlug(slug);
        contentDomain.setContent(content);
        contentDomain.setType(type);
        contentDomain.setStatus(status);
        contentDomain.setTags(type.equals(Types.ARTICLE.getType()) ? tags : null);  //只有博客才有标签
        //只允许博客文章有分类，防止作品被收入分类
        contentDomain.setCategories(type.equals(Types.ARTICLE.getType()) ? categories : null);  //只有博客才有分类
        contentDomain.setAllowComment(allowComment ? 1 : 0);


        //添加文章
        contentService.addArticle(contentDomain);


        return APIResponse.success();


    }

}
